﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using Newtonsoft.Json;
using System.Data;
using Model;
using System.Web.Script.Serialization;

namespace WebApplication01.AssetHandel
{
    /// <summary>
    /// RoleHandler 的摘要说明
    /// </summary>
    public class RoleHandler : IHttpHandler
    {
        RoleBLL rolebll = new RoleBLL();
        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Write("Hello World");
            string Stype = context.Request["sType"];
            string result = string.Empty;
            if (Stype == "list") {
                string roleName = context.Request["roleName"];
                int offset = Convert.ToInt32(context.Request["offset"]);
                int limit = Convert.ToInt32(context.Request["limit"]);
                int begin = offset + 1;
                int end = offset + limit;
                int total;
                List<RoleInfo> list = rolebll.GetRole(roleName, begin, end, out total);
                JavaScriptSerializer s = new JavaScriptSerializer();
                var json = new
                {
                    total = total,
                    rows = list
                };
                result = s.Serialize(json);
                //result = JsonConvert.SerializeObject(list);
                context.Response.Write(result);
            }
            else if (Stype == "select")
            {
                string role = context.Request["role"];
                List<RoleInfo> list = rolebll.BindRole(role);
                result = JsonConvert.SerializeObject(list);
                context.Response.Write(result);
            }
            else if (Stype == "save") {
                RoleInfo role = new RoleInfo();
                role.RoleName = context.Request["rName"];
                role.RoleReMark = context.Request["ReMark"];
                if (!string.IsNullOrEmpty(context.Request["ID"]))
                {
                    //编辑
                    role.RoleID = Convert.ToInt32(context.Request["ID"]);
                    result = rolebll.Update(role);
                }
                else {
                    //添加
                    result = rolebll.Add(role);
                }
                context.Response.Write(result);
            }
            else if (Stype == "Delete") {
                int rid = Convert.ToInt32(context.Request["ID"]);
                result = rolebll.Delete(rid);
                context.Response.Write(result);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}